//
//  CXAFNHttpRequestUtils.m
//  base_test
//
//  Created by  androidlongs on 17/8/15.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "CXAFNHttpRequestUtils.h"

@implementation CXAFNHttpRequestUtils

static CXAFNHttpRequestUtils *_requestUtils = nil;

+(CXAFNHttpRequestUtils *)sharedHttpRequestUtils{
    if(!_requestUtils){
        _requestUtils = [self init];
    }
    return _requestUtils;
}

//get请求无参数
+(void)httpPostRequest:(NSString *)url withParamters:(NSDictionary *)dic success:(void (^)(id))success failure:(void (^)(NSError *, NSInteger))failure{
    //监测网络变化
    YYReachability *reachable = [[YYReachability alloc] init];
    
    if (reachable.status == YYReachabilityStatusNone) {
        DLog(@"无可用网络");
        NSError *error = [[NSError alloc]init];
        failure(error,100);
        return;
    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //text/xml
    //application/json
    [manager.requestSerializer setValue:@"text/json" forHTTPHeaderField:@"Accept"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    
    [manager POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (success != nil) {
            success(responseObject);
        }else{
            
        }
    }
          failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
              
              if (failure != nil) {
                  failure(error,102);
              }
              
          }];
}

//get请求有参数
+(void)httpGetRequest:(NSString *)url withParamters:(NSDictionary *)dic success:(void (^)(id))success failure:(void (^)(NSError *,NSInteger errCode))failure{
    //监测网络变化
    YYReachability *reachable = [[YYReachability alloc] init];
    
    if (reachable.status == YYReachabilityStatusNone) {
        DLog(@"无可用网络");
        NSError *error = [[NSError alloc]init];
        failure(error,100);
        return;
    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"text/json" forHTTPHeaderField:@"Accept"];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    
    [manager GET:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success != nil) {
            success(responseObject);
        }else{
            
        }
        
        
    }
         failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
             
             if (failure != nil) {
                 failure(error,102);
             }else{
                 
             }
             
         }];}

//get请求无参数
+(void)httpGetRequest:(NSString *)url success:(void (^)(id))success failure:(void (^)(NSError *,NSInteger errCode))failure{
    [self httpGetRequest:url withParamters:nil success:success failure:failure];
}


//上传图片
+(void)httpPostFileRequestWithParamters:(NSDictionary *)dic withImage:(UIImage*)image success:(void (^)(id))success failure:(void (^)(NSError *, NSInteger))failure{
    
    NSString *url =[NSString stringWithFormat:@"%@%@",baseUrl,uploadImgUrl];
    
    //监测网络变化
    YYReachability *reachable = [[YYReachability alloc] init];
    
    if (reachable.status == YYReachabilityStatusNone) {
        DLog(@"无可用网络");
        NSError *error = [[NSError alloc]init];
        failure(error,100);
        return;
    }
    
    
    
    //2. 利用时间戳当做图片名字
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *imageName = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.jpg",imageName];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer setValue:@"text/json" forHTTPHeaderField:@"Accept"];
    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    
    [manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        // 上传图片，以文件流的格式，这里注意：name是指服务器端的文件夹名字
        [formData appendPartWithFileData:imageData name:@"img" fileName:fileName mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success != nil) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure != nil) {
            failure(error,102);
        }
    }];
    
}
//上传多图片
+(void)httpPostFileRequestWithParamters:(NSDictionary *)dic withImageArray:(NSMutableArray*)imageArray success:(void (^)(id))success failure:(void (^)(NSError * error, NSInteger code))failure{
    
    NSString *url =[NSString stringWithFormat:@"%@%@",baseUrl,uploadImgUrl];
    
    //监测网络变化
    YYReachability *reachable = [[YYReachability alloc] init];
    
    if (reachable.status == YYReachabilityStatusNone) {
        DLog(@"无可用网络");
        NSError *error = [[NSError alloc]init];
        failure(error,100);
        return;
    }
    
    
    
    //2. 利用时间戳当做图片名字
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *imageName = [formatter stringFromDate:[NSDate date]];
    
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.requestSerializer setValue:@"text/json" forHTTPHeaderField:@"Accept"];
    
    
    [manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        // 上传图片，以文件流的格式，这里注意：name是指服务器端的文件夹名字
        // 上传多张图片
        for(NSInteger i = 0; i < imageArray.count; i++)
        {
            //取出单张图片二进制数据
            UIImage * image = imageArray[i];
            NSData *imageData = UIImageJPEGRepresentation(image, 1);
            // 上传的参数名，在服务器端保存文件的文件夹名
            NSString * Name = [NSString stringWithFormat:@"%@%ld", imageName, i+1];
            // 上传filename
            NSString * fileName = [NSString stringWithFormat:@"%@.jpg", Name];
            
            [formData appendPartWithFileData:imageData name:@"img" fileName:fileName mimeType:@"image/jpeg"];
        }
        
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success != nil) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure != nil) {
            failure(error,102);
        }
    }];
    
}



@end
